var sceneList = new function(){
	
	var   _sceneContainer
		, _selected
	    , _scenes = []
		, _sceneNumber = 1
		, _nextSceneBtn
		, _prevSceneBtn
		, _sceneButtonsContainer
		, _openingScene
	;
	
	var _SCENE_ITEM_TEMPLATE = "<img class='imgSnapshot' src='img/claqueta.jpg'>";
	
	function _init(){
		
		_sceneContainer = $('#sceneList');
		_nextSceneBtn = $('#nextButtonScene');
		_prevSceneBtn = $('#previousButtonScene');
		_sceneButtonsContainer = $('#sceneButtonsContainer');
		_openingScene = $('#opening-scene');
		
		//opening scene selector
		_openingScene
			.kendoDropDownList({
				dataTextField: "text",
				dataValueField: "value",
			})
			.data("kendoDropDownList");
	
		_addScene();
		_scenes[0].item.click();
		
		$("#snapshotButton").click(function(){
			
			var auxImgData = mainCanvas.canvas.toDataURL("png");
			_selected.item.find('img.imgSnapshot').attr("src", auxImgData);
			_selected.snapshot = auxImgData;
		});
		
		 $('#newSceneButton').click(function(){
		
			 _addScene();
		 })
		
		 
		 $('#removeSceneButton').click(function(){
			
			 _removeSelectedScene();
		 })
		 
		//next previous scene buttons
		_prevSceneBtn.change(function(){
			sceneList.selected().prevBtn = ($(this).attr('checked') == 'checked') ? true : false;
		})
		_nextSceneBtn.change(function(){
			sceneList.selected().nextBtn = ($(this).attr('checked') == 'checked') ? true : false;
		})
		
	}
	
	function _addScene(sceneName, scene){
		
		if (!sceneName){
			sceneName = 'escena ' + _sceneNumber;
		}
		
		var snapshot = (scene) ? scene.snapshot : null;
		var batchNodes = (scene) ? scene.batchNodes : null;
		var newScene = new _scene(_sceneNumber, sceneName, snapshot, batchNodes)
		
		_scenes.push( newScene );
		
		var dataSource = _openingScene.data("kendoDropDownList").dataSource;
		dataSource.add({text:sceneName, value:sceneName, id:newScene.id});
		
		_sceneNumber++;
		
		return newScene;
	}
	
	function _scene(n, name, p_snapshot,p_batchNodes){
		
		var _this = this;
		var _id;
		var _tree = null;
		var _canvas = null;
		var _item = null;
		var _prevBtn = true;
		var _nextBtn = true;
		var _snapshot = null;
		var _batchNodes = null;
		
		function _initScene(n){
			
			_id = 'scene_' + n;
			_tree = new leftTreeConstructor();
			_tree.init(n);
			_canvas = new mainCanvasConstructor();
			_canvas.init(n);
			_createItem(n, name, p_snapshot);
			_nextSceneBtn.attr('checked', _nextBtn);
			_prevSceneBtn.attr('checked', _prevBtn);
			_snapshot = p_snapshot;
			_batchNodes = batchNodeController.createBatchNode4Scene();
			_batchNodes.set(p_batchNodes);
			
		}
		
		function _createItem(n, name, p_snapshot){
		
			var newitem = $('<li>')
				.attr('id', 'scene_' + n)
				.attr('sceneName', name)
				.addClass('scene-list-item')
				.html(_SCENE_ITEM_TEMPLATE);
			
			_sceneContainer.append(newitem);
			 
			if (n != 1){
				$("#new-tree-" + n).hide(); 
				$("#new-canvas-" + n).parent().hide();
			}
			
			//incluir snapshot
			if(p_snapshot){
				newitem.find('img.imgSnapshot').attr("src", p_snapshot);
			}
			 
			newitem.click(function(){
				
				if (_selected) {
					//_scenes[_selected].tree.saveCurrentCanvas();
					sliderController.disableAll(); 
					mainCanvas.canvas.deactivateAll();
					mainCanvas.canvas.renderAll();
					leftTree.unselectAllNodes();
					_selected.item.toggleClass('selected');
				}
				_selected = _this;
				leftTree = _selected.tree;
				mainCanvas = _selected.canvas;
				//mainCanvas.loadScene();
				_selected.item.toggleClass('selected');
				
				$("#treeview-left").children().hide();
				$("#new-tree-" + n).show();
				$('#canvasWrapper').children().hide();
				$("#new-canvas-" + n).parent().show();
				
				mainCanvas.canvas.calcOffset();
				
				$('#scene-name-input').val($(this).attr('sceneName'));
				_nextSceneBtn.attr('checked', _selected.nextBtn);
				_prevSceneBtn.attr('checked', _selected.prevBtn);
				_sceneButtonsContainer.show();
				
				//fill batch node selector
				batchNodeController.updateSelector();
			})
			 
			_item = newitem;
		}
		
		_initScene(n);
		
		this.id = _id;
		this.tree = _tree;
		this.item = _item;
		this.canvas = _canvas;
		this.prevBtn = _prevBtn;
		this.nextBtn = _nextBtn;
		this.snapshot = _snapshot;
		this.batchNodes = _batchNodes;
		
		return this;
	}
	
	function _removeSelectedScene(){
		
		if (_selected){
			_removeScene(_selected);	
		}
	}
	
	function _removeScene(s){
		
		//remove from opening scene selector
		var id = s.id;
		var kOpening = _openingScene.data("kendoDropDownList");
		
		if (kOpening.value() == s.id){
			kOpening.value("");
			kOpening.text("");
		}
		
		var dataSource = kOpening.dataSource;
		var item = dataSource.get(id);
		dataSource.remove(item);	
		
		s.tree.treeView.element.remove();
		$(s.canvas.canvas.wrapperEl).remove();
		s.item.remove();
		if (s == _selected){
			$("#scene-name-input").val('');
			_selected = null;
			sliderController.disableAll();
		}
		_scenes.splice( $.inArray(s, _scenes), 1);
		
		_sceneButtonsContainer.hide();
	}
	
	function _removeAllScenes(){
		
		stateController.empty();
		actionController.spawnContainer = {};
	
		while (_scenes.length){
			_removeScene( _scenes[0] );
		}
		_sceneNumber = 1;
	}
	
	function _getNextScene(){
		
		var   currentId = _selected.id
			, next = ''
			, l = _scenes.length
			, i = 0
		;
		
		while (!next && i < l-1){
			
			if (_scenes[i].id == currentId){
				next = _scenes[i+1].item.attr('scenename');
			}
			i++;
		}
		
		return next;
	}
	
	function _getPrevScene(){
		
		var   currentId = _selected.id
			, prev = ''
			, l = _scenes.length
			, i = 1
		;
		
		while (!prev && i < l){
			
			if (_scenes[i].id == currentId){
				prev = _scenes[i-1].item.attr('scenename');
			}
			i++;
		}
		
		return prev;
		
	}

	
	this.scenes = _scenes;
	this.selected = function(){ return _selected};
	this.addScene = _addScene;
	this.removeScene = _removeScene;
	this.removeAllScenes = _removeAllScenes;
	this.getNextScene = _getNextScene;
	this.getPrevScene = _getPrevScene;
	

	this.init = _init;
	
}


